<%= form_for @image, url: (@url_override or @image.persisted? ? refinery.admin_image_path(@image) : refinery.admin_images_path),
             html: { multipart: true } do |f| %>

  <%= render '/refinery/admin/error_messages',
             object: @image,
             include_object_name: false %>

  <%= render '/refinery/admin/locale_picker', :current_locale => Mobility.locale if @image.persisted? %>

  <div class="field">
    <% if action_name =~ /(edit)|(update)/ %>
      <p>
        <%= t('.use_current_image') %>
        <em><%= t('.or') %></em><%= t('.replace_image') %>
      </p>
      <p>
        <%= f.file_field :image %>
      </p>
    <% else %>
      <% # we must only hint at multiple when it's a new record otherwise update fails. %>
      <%= f.file_field :image, multiple: true %>
    <% end %>
  </div>

  <div class="field">
    <label><%= t('.maximum_image_size', bytes: number_to_human_size(Refinery::Images.max_image_size)) %></label>
  </div>
  <div class="field" id="title">
    <span class='label_with_help'>
      <%= f.label :image_title, t('.image_title') %>
      <%= refinery_help_tag t('.image_title_help') %>
    </span>
    <%= f.text_field :image_title, placeholder: t('.image_title') %>
  </div>

  <div class="field" id="alt">
    <span class='label_with_help'>
      <%= f.label :image_alt, t('.image_alt') %>
      <%= refinery_help_tag t('.image_alt_help') %>
    </span>
    <%= f.text_field :image_alt, placeholder: t('.image_alt') %>
  </div>

  <input type="hidden" name="wymeditor" value="<%= params[:wymeditor] %>">

  <%= render '/refinery/admin/form_actions', f: f,
             continue_editing: false,
             hide_cancel: (@app_dialog or action_name == 'insert' or from_dialog?),
             delete_title: t('delete', scope: 'refinery.admin.images'),
             delete_confirmation: t('message', scope: 'refinery.admin.delete', title: @image.image_name),
             cancel_url: refinery.admin_images_path -%>

  <% if @app_dialog %>
    <input type="hidden" name="app_dialog" value="<%= @app_dialog %>" />
    <input type="hidden" name="field" value="<%= @field %>" />
    <input type="hidden" name="update_image" value="<%= @update_image %>" />
    <input type="hidden" name="thumbnail" value="<%= @thumbnail %>" />
    <input type="hidden" name="callback" value="<%= @callback %>" />
    <input type="hidden" name="conditions" value="<%= @conditions %>" />
  <% end %>
<% end %>

<% if action_name =~ /(edit)|(update)/ %>
  <div id="existing_image">
    <label><%= t('.current_image') %></label>
    <div id="original-image">
      <%= image_tag @image.url, id: 'crop',
                  data: { mime_type: @image.mime_type,
                          filename: @image.image_name,
                          image_id: @image.id } %>
    </div>

    <section id="ratios" class="clearfix">
      <%= form_for @image,
                   url: refinery.admin_image_crop_path(@image),
                   html: {
                     multipart: true,
                     id: "crop_image_#{@image.id}",
                     class: 'crop_image'
                   } do |f| %>
        <div class="field">
          <span class='label_with_help'>
            <%= f.label :ratio, t('.ratios') %>
            <%= refinery_help_tag t('.choose_crop') %>
          </span>

          <% ::Refinery::Images.user_image_ratios.each do |ratio, value| %>
            <%= f.button ratio, type: 'button', data: { value: value, ratio: ratio.to_s.parameterize } %>
          <% end %>

          <%= f.submit t('.save'), id: 'save_crop' %>
        </div>
      <% end %>
    </section>

    <section id="crops">
      <h2><%= t('.available_crops') %></h2>

      <% if @image.crops.present? %>
        <ul id="crop-list" class="clearfix">
          <%= render partial: 'crop', collection: @image.crops %>
        </ul>
      <% else %>
          <p id="no-crops-yet"><strong><%=t('.no_crops_yet')%></strong></p>
      <% end %>
    </section>
  </div>

  <% content_for :after_javascript_libraries do %>
    <%= javascript_include_tag 'cropper' %>
    <%= javascript_include_tag 'canvas-to-blob' %>
    <%= javascript_include_tag 'refinery/image_crop' %>
  <% end %>

  <% content_for :stylesheets, stylesheet_link_tag('cropper') -%>
<% end %>